package tool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Database class 
 * @author Simon Hiemstra
 * @version 1
 */
public class Database 
{
	// Database connection singleton
	public static Connection VKDB;

	// Database details
	private static String dbUsername = "teamx";
	private static String dbPassword = "teamx";
	public static String dbms = "derby";
	public static String dbName = "database";
	public static String dbLocation = "//localhost:1527/D:\\Projects\\VossenEnKonijnen\\"; // Charlotte
	// Database schema
	public static String Schema = "VK";
	
	/**
	 * Apply the static singleton to the database connection
	 * @throws SQLException
	 */
	public static void getConnection() throws SQLException 
	{
	    Connection conn = null;
	    Properties connectionProps = new Properties();
	    connectionProps.put("user", dbUsername);
	    connectionProps.put("password", dbPassword);

	    VKDB = DriverManager.getConnection("jdbc:"+dbms+":"+dbLocation+dbName, connectionProps);

	    // System.out.println("Connected to database");
	}
	
	/**
	 * Perform a database query and return the result as an ResultSet
	 * @param query
	 * @return ResultSet or null if the query failed.
	 */
	public static ResultSet query(String query)
	{
		try 
		{
			PreparedStatement statement = Database.VKDB.prepareStatement(query);
			return statement.executeQuery();
		} 
		catch (Exception e) 
		{
			System.out.println("Query failed.");
			//e.printStackTrace();
		} 
		
		return null;
	}
}